package net.piggy.core.util;


import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Method;

public class ParamUtil {
    private ParamUtil(){}

    public static Object[] getParam(String parameterNames, JSONObject params, String parameterTypes) throws Exception {
        String[] parameterTypeArray = parameterTypes.split(",");
        if(parameterTypeArray.length==1){
            if(SimpleTypeUtil.isSimpleType(parameterTypeArray[0])){
                String param = params.get(parameterNames).toString();
                String type = parameterTypeArray[0];
                if(!"java.lang.String".equals(type)){
                    Class<?> clazz = Class.forName(type);
                    Method methodObj = clazz.getMethod("valueOf", String.class);
                    Object arg = methodObj.invoke(null, param);
                    return new Object[]{arg};
                }else{
                    return new Object[]{param};
                }
            }else{
                return new Object[]{params};
            }
        }
        throw new Exception("analysis param fail");
    }

}
